var t = function(t) {
    var e = t.toLowerCase();
    if (e && /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(e)) {
        if (4 === e.length) {
            for (var a = "#", s = 1; s < 4; s += 1) a += e.slice(s, s + 1).concat(e.slice(s, s + 1));
            e = a;
        }
        for (var i = [], r = 1; r < 7; r += 2) i.push(parseInt("0x" + e.slice(r, r + 2)));
        return "rgb(" + i.join(",") + ")";
    }
    return e;
}, e = function(t) {
    if (/^(rgb|RGB)/.test(t)) {
        for (var e = t.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(","), a = "#", s = 0; s < e.length; s++) {
            var i = Number(e[s]).toString(16);
            i.length < 2 && (i = "0" + i), a += i;
        }
        return 7 !== a.length && (a = that), a;
    }
    if (/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(t)) {
        var r = t.replace(/#/, "").split("");
        if (6 === r.length) return t;
        if (3 === r.length) {
            for (var o = "#", h = 0; h < r.length; h += 1) o += r[h] + r[h];
            return o;
        }
    }
    return t;
};

Component({
    properties: {
        initColor: {
            type: String,
            value: "rgb(255,0,0)"
        },
        moduleHost: {
            type: String,
            value: ""
        },
        maskClosable: {
            type: Boolean,
            value: !0
        },
        mask: {
            type: Boolean,
            value: !0
        },
        show: {
            type: Boolean,
            value: !1
        }
    },
    observers: {
        "show, initColor": function(t, e) {
            t && (this.initAttached(), this.initReady());
        }
    },
    data: {
        color: ""
    },
    lifetimes: {
        attached: function() {
            this.initAttached();
        },
        ready: function() {
            this.initReady();
        }
    },
    methods: {
        initAttached: function() {
            var e = this.data.initColor, a = e;
            /#/.test(e) && (a = t(e)), this.setData({
                color: a
            }), this.setData({
                hueColor: this.hsv2rgb(this.rgb2hsv(a).h, 100, 100)
            });
        },
        initReady: function() {
            var e = this, a = this.createSelectorQuery();
            a.select(".target").boundingClientRect(), a.exec(function(a) {
                var s, i = a[0];
                i && (e.SV = {
                    W: i.width - 38,
                    H: i.height - 38,
                    Step: (i.width - 38) / 100
                }, a = (s = e.rgb2hsv(t(e.data.initColor))).h, i = s.s, s = s.v, e.setData({
                    hsv: {
                        h: a,
                        s: i,
                        v: s
                    },
                    x: Math.round(i * e.SV.Step),
                    y: Math.round((100 - s) * e.SV.Step)
                }));
            });
        },
        onEnd: function(t) {
            this.setData({
                color: this.data.colorRes
            }), this.data.colorRes && this.triggerEvent("changeColor", {
                color: e(this.data.colorRes)
            });
        },
        changeHue: function(t) {
            var a = t.detail.value;
            t = this.hsv2rgb(a, this.data.hsv.s, this.data.hsv.v);
            this.setData({
                "hsv.h": a,
                hueColor: this.hsv2rgb(a, 100, 100),
                colorRes: this.hsv2rgb(a, this.data.hsv.s, this.data.hsv.v),
                color: t
            }), this.triggerEvent("changeColor", {
                color: e(t)
            });
        },
        changingHue: function(t) {
            t = t.detail.value, this.setData({
                "hsv.h": t,
                hueColor: this.hsv2rgb(t, 100, 100),
                colorRes: this.hsv2rgb(t, this.data.hsv.s, this.data.hsv.v)
            });
        },
        changeSV: function(t) {
            t = (e = t.detail).x;
            var e = e.y;
            t = Math.round(t / this.SV.Step), e = 100 - Math.round(e / this.SV.Step);
            this.setData({
                "hsv.s": t,
                "hsv.v": e,
                colorRes: this.hsv2rgb(this.data.hsv.h, t, e)
            });
        },
        close: function(t) {
            this.data.maskClosable && (this.setData({
                show: !1
            }), this.triggerEvent("close"));
        },
        confirm: function(t) {
            this.data.maskClosable && (this.setData({
                show: !1
            }), this.triggerEvent("confirm", e(this.data.color)));
        },
        preventdefault: function() {},
        hsv2rgb: function(t, e, a) {
            t = (t / 360).toFixed(2), e = (e / 100).toFixed(2);
            var s = (a / 100).toFixed(2), i = s * (1 - e), r = s * (1 - (t = 6 * t - (a = Math.floor(6 * t))) * e), o = s * (1 - (1 - t) * e), h = 0, n = 0, l = 0;
            switch (a % 6) {
              case 0:
                h = s, n = o, l = i;
                break;

              case 1:
                h = r, n = s, l = i;
                break;

              case 2:
                h = i, n = s, l = o;
                break;

              case 3:
                h = i, n = r, l = s;
                break;

              case 4:
                h = o, n = i, l = s;
                break;

              case 5:
                h = s, n = i, l = r;
            }
            return "rgb(" + Math.floor(255 * h) + "," + Math.floor(255 * n) + "," + Math.floor(255 * l) + ")";
        },
        rgb2hsv: function(t) {
            var e = t.split(","), a = parseInt(e[0].split("(")[1]) / 255, s = parseInt(e[1]) / 255, i = parseInt(e[2].split(")")[0]) / 255, r = Math.max(a, s, i), o = Math.min(a, s, i), h = void 0, n = (t = void 0, 
            e = r, r - o);
            t = 0 == r ? 0 : n / r;
            if (r == o) h = 0; else {
                switch (r) {
                  case a:
                    h = (s - i) / n + (s < i ? 6 : 0);
                    break;

                  case s:
                    h = (i - a) / n + 2;
                    break;

                  case i:
                    h = (a - s) / n + 4;
                }
                h /= 6;
            }
            return {
                h: (360 * h).toFixed(),
                s: (100 * t).toFixed(),
                v: (100 * e).toFixed()
            };
        }
    }
});